<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 * Description of GUsuario
 *
 * @author Luis Alberto
 */
include './libs/adodb/adodb.inc.php';
include './configuracion.php';
include 'Envio.php';
class GEnvio
{
    private $db;
    private $envio = null;
    public function __construct()
    {
        $this->envio = new Envio();
    }
    public function Nuevo()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select max(env_cod) as codigo from envio";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
            return $dato;
        }
        return null;
    }
    public function Guardar($desc,$hor,$fech,$tobj,$tabier,$usrcod,$camcod,$estcod,$cancod,$lmc_cod,$lme_cod)
    {        
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "insert into envio (env_descripcion, env_hora, env_fecha, env_total_objetivo, env_total_abiertos, ";
        $cns .= "usr_cod, cam_cod , est_cod, can_cod, lmc_cod, lme_cod)";
        $cns .= " values ('$desc','$hor','$fech',$tobj,$tabier,$usrcod,$camcod,$estcod,$cancod,$lmc_cod,$lme_cod)";
        //echo $cns;
        $rs = $db->Execute($cns);
        return $rs;
    }
    public function Modificar($desc,$hor,$fech,$tobj,$tabier,$usrcod,$camcod,$estcod,$cancod,$lmc_cod,$lme_cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "update envio";
        $cns = "set usr_name = $usrn, usr_password=$pass, usr_nombre=$nomb, usr_apellido=$apell, usr_direccion=$dir, usr_email=$ema, usr_telefono=$tel, usr_ci=$ci, per_cod=$per_co";
        $cns .= "where (usr_cod = $cod)";
	return $rs = $db->Execute($cns);
    }
    public function Eliminar($cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "delete from envio where env_cod=$cod";
	return $rs = $db->Execute($cns);
    }
    public function Obtener($cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select * from envio where env_cod = $cod";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
            $this->envio->setenv_cod($dato['env_cod']);
            $this->envio->setenv_descripcion($dato['env_descripcion']);
            $this->envio->setenv_hora($dato['env_hora']);
            $this->envio->setenv_fecha($dato['env_fecha']);
            $this->envio->setenv_total_objetivo($dato['env_total_objetivo']);
            $this->envio->setenv_total_abiertos($dato['env_total_abiertos']);
            $this->envio->setusr_cod($dato['usr_cod']);
            $this->envio->setcam_cod($dato['cam_cod']);
            $this->envio->setest_cod($dato['est_cod']);
            $this->envio->setcan_cod($dato['can_cod']);
            $this->envio->setlmc_cod($dato['lmc_cod']);
            $this->envio->setlme_cod($dato['lme_cod']);            
            return $this->envio;
        }
        return null;
    }
    public function Buscar($buscar, $campo)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        /*$cns = "select * from envio where $campo like '%$buscar%'";*/
        $cns = "select en.env_cod, en.env_descripcion, en.env_fecha, en.env_total_objetivo, u.usr_username, ca.cam_titulo, ese.est_descripcion, com.can_nombre from envio as en";
        $cns .= " INNER JOIN usuario u ON en.usr_cod = u.usr_cod";
        $cns .= " INNER JOIN campania as ca ON en.cam_cod = ca.cam_cod";
        $cns .= " INNER JOIN  estadoenvio as ese ON  en.est_cod = ese.est_cod";
        $cns .= " INNER JOIN  canalcomunicacion as com ON  en.can_cod = com.can_cod";
        $cns .= " where ($campo like '%$buscar%')";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function Seleccionar()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select en.env_cod, en.env_descripcion, en.env_fecha, en.env_total_objetivo, u.usr_username, ca.cam_titulo, ese.est_descripcion, com.can_nombre from envio as en";
        $cns .= " INNER JOIN usuario u ON en.usr_cod = u.usr_cod";
        $cns .= " INNER JOIN campania as ca ON en.cam_cod = ca.cam_cod";
        $cns .= " INNER JOIN  estadoenvio as ese ON  en.est_cod = ese.est_cod";
        $cns .= " INNER JOIN  canalcomunicacion as com ON  en.can_cod = com.can_cod";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function SeleccionarLetra($letra)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        /*$cns = "select * from envio where env_descripcion like '$letra%'";*/

        $cns = "select en.env_cod, en.env_descripcion, en.env_fecha, en.env_total_objetivo, u.usr_username, ca.cam_titulo, ese.est_descripcion, com.can_nombre from envio as en";
        $cns .= " INNER JOIN usuario u ON en.usr_cod = u.usr_cod";
        $cns .= " INNER JOIN campania as ca ON en.cam_cod = ca.cam_cod";
        $cns .= " INNER JOIN  estadoenvio as ese ON  en.est_cod = ese.est_cod";
        $cns .= " INNER JOIN  canalcomunicacion as com ON  en.can_cod = com.can_cod";
        $cns .= " where (env_descripcion like '$letra%')";

        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function  Obtenerestadoenvio()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from estadoenvio";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function  Obtenercanales()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from canalcomunicacion";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function Obtenercampanias()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from campania where est_cod=3";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function Obtenerlistacontactos()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from lista_email_contactos";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function Obtenerlistaempresas()
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $cns = "select * from lista_email_empresas";
        $rs = $db->Execute($cns);
        $l = $rs->GetRows();
        return $l;
    }
    public function totalObjetivo($lmccod,$lmecod)
    {
        $cont = 0;
        if($lmccod != "NULL")
        {
            $db = ADONewConnection(DRIVER);
            $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
            $cns = "select * from detalle_email_contactos where lmc_cod = $lmccod";
            $rs = $db->Execute($cns);
            $aux = $rs->RecordCount();
            $cont = $cont + $aux;
        }
        if($lmecod != "NULL")
        {
            $db = ADONewConnection(DRIVER);
            $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
            $cns = "select * from detalle_email_empresas where lme_cod = $lmecod";
            $rs = $db->Execute($cns);
            $aux = $rs->RecordCount();
            $cont = $cont + $aux;
        }
        return $cont;
    }
    public function getConfiguracionSocial($can_cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
	$cns = "select * from configuracionsocial where can_cod = $can_cod and cs_username = 'uibasoft'";
	$rs = $db->Execute($cns);
        if($rs)
        {
            $dato = $rs->fetchRow();
        }
        return $dato;
    }
    public function actualizaestado($cod,$est_cod)
    {
        $db = ADONewConnection(DRIVER);
        $db->Connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        $hora = date("H:i:s");
        $fecha = date("d/m/Y");
	$cns = "update envio";
        $cns .= " set env_hora = '$hora' , env_fecha='$fecha', est_cod = $est_cod ";
        $cns .= "where (env_cod = $cod)";
        //echo $cns;
	return $rs = $db->Execute($cns);
    }
}
?>
